package com.example.sh_crosses.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.sh_crosses.base.App;

import java.util.ArrayList;
import java.util.List;

public class UserDao {
    private final String TAG = "UserDao";
    public static final String TABLE_NAME = "user";
    private static final String ID = "id";
    private static final String NAME = "user_name";
    private static final String PASS = "user_pass";
    private DBHelper dbHelper;

    //创建表结构
    public static final String SQL_CREATE_TABLE = "create table " + TABLE_NAME + "(" +
            ID + " integer primary key autoincrement," +
            NAME + " text," +
            PASS + " text" +
            ")";


    private UserDao() {
        dbHelper = new DBHelper(App.getContext());
    }

    public static UserDao getInstance() {
        return InnerDb.instance;
    }
    private static class InnerDb{
        private static UserDao instance = new UserDao();
    }

    /**
     * 修改表中表中所有的数据
     */
    public synchronized void update(String id,String pass) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues value = new ContentValues();
        value.put(PASS,pass);
        db.update(TABLE_NAME,value,ID+"=?",new String[]{id});

//        try {
//            db.execSQL(sql);
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            db.close();
//        }
    }


    /**
     * 数据库插入数据
     *
     */
    public synchronized <T> int insert(String name,String pass) {
        Log.e(TAG, "insert: " );
        int ret = -1;
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {
            ContentValues cv = new ContentValues();
            cv.put(NAME,name);
            cv.put(PASS,pass);
            db.insert(TABLE_NAME, null, cv);
            ret = 1;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
        return ret;
    }

    /**
     * 删除表中所有的数据
     */
    public synchronized void clearAll() {
        Log.e(TAG, "clearAll: " );
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql = "delete from " + TABLE_NAME;

        try {
            db.execSQL(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.close();
        }
    }
    /**
     * 查询数据
     *
     * @return List
     */
    @SuppressLint("Range")
    public synchronized <T> List<T> query(String name,String pass) {
        Log.e(TAG, "query: " );
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        List<T> list = new ArrayList<>();
        String querySql = "select * from " + TABLE_NAME;
        Cursor cursor = null;
        try {
            cursor = db.rawQuery(querySql, null);
            while (cursor.moveToNext()) {
                Log.e(NAME,"value:"+cursor.getString(cursor.getColumnIndex(NAME)));
                Log.e(PASS,"value:"+cursor.getString(cursor.getColumnIndex(PASS)));
                if(cursor.getString(cursor.getColumnIndex(NAME)).equals(name)&&cursor.getString(cursor.getColumnIndex(PASS)).equals(pass)){
                    Log.e(NAME+"-"+PASS,"验证成功");
                    list.add((T) cursor.getString(cursor.getColumnIndex(NAME)));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return list;
    }
}
